import React from "react";
import Button from 'antd/lib/button';
import Divider from 'antd/lib/divider';
import Menu from 'antd/lib/menu';
import Modal from 'antd/lib/modal';
import Enums from "@/services/enums";
import ProjectTable from "@/components/project/ProjectTable";
import ProjectAttache from "@/components/project/ProjectAttache";
import ProjectDetailList from "@/components/project/ProjectDetailList";
import notification from "@/services/notification";
import { currentUser } from "@/services/auth";
import Customer from "@/services/customer";
import WarningOutlined from "@ant-design/icons/WarningOutlined";
import EmployeeFollowUp from "@/components/employee/EmployeeFollowUp";
import Lookcheck from "@/components/workflow/Lookcheck";
import { createBrowserHistory } from 'history';
import OrgUnit from "@/services/orgunit";
import { inject, observer } from 'mobx-react';

const customHistory = createBrowserHistory();
const { confirm } = Modal;

class ProjectToolbar extends React.Component {
  state = {
    loading: true,
    openpage: "jbxx",
    projectstatusdata: [],
    employeemajordata: [],
    employeecertificatedata: [],
    employeeareadata: [],
    employeeuserdata: [],
    orgunitdata: [],
    customerdata: [],
  };
  componentWillMount() {
    Enums.items("projectstatus").then(data => {
      this.setState({
        projectstatusdata: data,
        openpage:this.props.openpage,
        loading: false,
      });
    });
    Enums.items("employeemajor").then(data => {
      this.setState({
        employeemajordata: data,
      });
    });
    Enums.items("employeecertificate").then(data => {
      this.setState({
        employeecertificatedata: data,
      });
    });
    Enums.items("employeearea").then(data => {
      this.setState({
        employeeareadata: data,
      });
    });

    Customer.getcustomerlist({ page: 1, pagesize: 10000, strcustomercode: '', strcustomername: '', userid: currentUser.id, }).then(data => {
      this.setState({
        customerdata: data.data,
      });
    });
    Customer.querycustomeruser({ code: "", name: "", userid: currentUser.id, }).then(data => {
      this.setState({
        employeeuserdata: data,
      });
    });
    OrgUnit.queryActive().then(data => {
      this.setState({
        orgunitdata: data,
      });
    });
    this.refreshData(this.props.id);
  }

  refreshData(id) {
    this.props.projectStore.initStore(id);
  }
  handleadd = () => {
      this.refreshData(0);
  };

  handleSave = () => {
    this.props.projectStore.dataSave();
  };
  handleDelete = () => {
    if (this.props.projectStore.projectModel.userid !== currentUser.id) {
      notification.error("不是创建人，不能删除！");
      return;
    }
    confirm({
      title: '确定删除项目资料?',
      icon: <WarningOutlined />,
      content: this.props.projectStore.projectModel.code,
      okText: '确定',
      okType: 'danger',
      cancelText: '取消',
      onOk: () => {
        this.props.projectStore.dataDelete();
      },
    });
  };

  handlebackpage = () => {
    customHistory.goBack();
  };
  handleCheck = () => {
    if (!currentUser.isAudit) {
      notification.error("没有权限.");
      return;
    }
    this.props.projectStore.billCheck(currentUser.id);
  };
  tabsClick = e => {
    this.setState({
      openpage: e.key,
    });
  };

  templateSelect() {

    if (this.state.openpage === "jbxx") {
      return (<ProjectTable
        projectstatusdata={this.state.projectstatusdata} employeemajordata={this.state.employeemajordata}
        employeecertificatedata={this.state.employeecertificatedata} employeeareadata={this.state.employeeareadata}
        employeeuserdata={this.state.employeeuserdata} customerdata={this.state.customerdata}
        orgunitdata={this.state.orgunitdata} />);
    } else if (this.state.openpage === "gjxx") {
      return (<EmployeeFollowUp 
        isedit={this.props.projectStore.projectModel.isedit} colcontrol={this.props.projectStore.projectModel.colcontrol}
        allow_col={this.props.projectStore.projectModel.allow_col} status={this.props.projectStore.projectModel.status}
        id={this.props.projectStore.projectModel.id} sourcetype="project" employeeuserdata={this.state.employeeuserdata} />);
    } else if (this.state.openpage === "ryxx") {
      return (<ProjectDetailList />);
    } else if (this.state.openpage === "zlsc") {
      return (<ProjectAttache id={this.props.projectStore.projectModel.id} 
        isedit={this.props.projectStore.projectModel.isedit} colcontrol={this.props.projectStore.projectModel.colcontrol}
        allow_col={this.props.projectStore.projectModel.allow_col} status={this.props.projectStore.projectModel.status}/>);
    } else if(this.state.openpage === "spxx") {
      return (<Lookcheck  bill="project"  billid={this.props.projectStore.projectModel.id} bill_type ={0}
      orgunitid ={this.props.projectStore.projectModel.orgunitid}
      />);
    }
  };
  render() {
    if (this.state.loading) return null;
    window.console.log('this.state.status', this.state.status)
    return (
      <div>
        <div style={{ marginBottom: 16 }}>
          <Button type="primary" style={{ margin: "10px" }} onClick={() => this.handlebackpage()}>
            <i className="fa fa-reply m-r-5" />
            返回
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleadd()}>
            <i className="fa fa-plus m-r-5" />
            新增
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleSave()} disabled={!this.props.projectStore.projectModel.isedit}>
            <i className="fa fa-save m-r-5" />
            保存
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleDelete()}  disabled={this.props.projectStore.projectModel.status !==0}>
            <i className="fa fa-trash m-r-5" />
            删除
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleCheck()}>
            <i className="fa fa-check m-r-5" />
            {this.state.status === 0 ? "提交" : "审核"}
          </Button>
        </div>
        <div>
          <Menu onClick={this.tabsClick} selectedKeys={this.state.openpage} mode="horizontal">
            {currentUser.hasPermission("projectmanage") &&(<Menu.Item key="jbxx">基本信息</Menu.Item>)}
            <Menu.Item key="gjxx">跟进信息</Menu.Item>
            <Menu.Item key="ryxx">人员信息</Menu.Item>
            <Menu.Item key="zlsc">合同上传</Menu.Item>
            <Menu.Item key="spxx">审批信息</Menu.Item>
          </Menu>
          {this.templateSelect()}
        </div>
      </div>
    );
  };
}
export default inject('projectStore')(observer(ProjectToolbar));